load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")

filegroup(
    name = "flex_lexer_h",
    srcs = ["src/FlexLexer.h"],
    visibility = ["//:__subpackages__"],
)

COMMON_COPTS = [
    "-w",
]

COMMON_DEFINES = [
    "HAVE_ASSERT_H=1",
    "HAVE_LIMITS_H=1",
    "HAVE_NETINET_IN_H=1",
    "HAVE_REGEX_H=1",
    "HAVE_SYS_STAT_H=1",
    "HAVE_SYS_WAIT_H=1",
    "HAVE_UNISTD_H=1",
    "STDC_HEADERS=1",
    "VERSION=\\\"2.6.4\\\"",
    "M4=\\\"/bin/false\\\"",
] + select({
    "@platforms//os:macos": [
        "HAVE_ALLOCA=1",
        "HAVE_ALLOCA_H=1",
        "HAVE_CFLOCALECOPYCURRENT=1",
        "HAVE_CFPREFERENCESCOPYAPPVALUE=1",
        "HAVE_DLFCN_H=1",
        "HAVE_DUP2=1",
        "HAVE_FORK=1",
        "HAVE_ICONV=1",
        "HAVE_INTTYPES_H=1",
        "HAVE_LIBM=1",
        "HAVE_LOCALE_H=1",
        "HAVE_MALLOC=1",
        "HAVE_MEMORY_H=1",
        "HAVE_MEMSET=1",
        "HAVE_POW=1",
        "HAVE_PTHREAD_H=1",
        "HAVE_REALLOC=1",
        "HAVE_REGCOMP=1",
        "HAVE_SETLOCALE=1",
        "HAVE_STDBOOL_H=1",
        "HAVE_STDINT_H=1",
        "HAVE_STDLIB_H=1",
        "HAVE_STRCASECMP=1",
        "HAVE_STRCHR=1",
        "HAVE_STRDUP=1",
        "HAVE_STRINGS_H=1",
        "HAVE_STRING_H=1",
        "HAVE_STRTOL=1",
        "HAVE_SYS_TYPES_H=1",
        "HAVE_VFORK=1",
        "HAVE_WORKING_FORK=1",
        "HAVE_WORKING_VFORK=1",
        "HAVE__BOOL=1",
        "YYTEXT_POINTER=1",
    ],
    "//conditions:default": [
    ],
})

FLEX_SRCS = glob(
    include = [
        "src/*.c",
        "src/*.h",
    ],
    exclude = [
        "src/main.c",
        "src/lib*.c",
    ],
)

cc_library(
    name = "flex_lib",
    srcs = FLEX_SRCS,
    copts = COMMON_COPTS,
    defines = COMMON_DEFINES,
    features = ["no_copts_tokenization"],
)

filegroup(
    name = "flex_runfiles",
    srcs = [
        "@rules_m4//m4:current_m4_toolchain",
    ],
)

cc_binary(
    name = "flex",
    srcs = ["src/main.c"],
    copts = COMMON_COPTS,
    data = [":flex_runfiles"],
    features = ["-default_link_libs"],
    linkopts = [],
    visibility = ["//visibility:public"],
    deps = [":flex_lib"],
)

cc_library(
    name = "flex_lexer_hdrs",
    hdrs = ["src/FlexLexer.h"],
    includes = ["src"],
    visibility = ["//visibility:public"],
)
